package flyingpig.data.dao;
// default package
// Generated 2010-5-2 9:39:48 by Hibernate Tools 3.3.0.GA


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;

import flyingpig.data.domain.SearchJob;
import flyingpig.hibernate.HibernateUtil;

/**
 * Home object for domain model class SearchJob.
 * @see .SearchJob
 * @author Hibernate Tools
 */
public class SearchJobHome {

	private static final Log log = LogFactory.getLog(SearchJobHome.class);

	private final SessionFactory sessionFactory = getSessionFactory();

	protected SessionFactory getSessionFactory() {
		try {
			return HibernateUtil.getSessionFactory();
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	public void persist(SearchJob transientInstance) {
		log.debug("persisting SearchJob instance");
		try {
			HibernateUtil.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(SearchJob instance) {
		log.debug("attaching dirty SearchJob instance");
		try {
			HibernateUtil.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(SearchJob instance) {
		log.debug("attaching clean SearchJob instance");
		try {
			HibernateUtil.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(SearchJob persistentInstance) {
		log.debug("deleting SearchJob instance");
		try {
			HibernateUtil.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public SearchJob merge(SearchJob detachedInstance) {
		log.debug("merging SearchJob instance");
		try {
			SearchJob result = (SearchJob) HibernateUtil.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public SearchJob findById(java.lang.Integer id) {
		log.debug("getting SearchJob instance with id: " + id);
		try {
			SearchJob instance = (SearchJob) HibernateUtil.getCurrentSession()
					.get("flyingpig.data.domain.SearchJob", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(SearchJob instance) {
		log.debug("finding SearchJob instance by example");
		try {
			List results = HibernateUtil.getCurrentSession().createCriteria(
					"flyingpig.data.domain.SearchJob").add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public List<SearchJob> findSearchJobs(int max){
		Query q = HibernateUtil.getCurrentSession().createQuery("from SearchJob as S");
		q.setFirstResult(0); 
		q.setMaxResults(max); 
		
		List<SearchJob> searchJobs = new ArrayList<SearchJob>();
    	for(int i=0;i<q.list().size();i++){
    		Object ob = q.list().get(i);
    		searchJobs.add((SearchJob)ob);   		
    	}
    	
    	return searchJobs;
	}
}
